﻿using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading;

namespace WISBenchmarkClient
{
    class Client
    {
        private readonly double _upper = 1.2;
        private readonly double _lower = 0.8;
        private Func<HttpClient, Boolean> RequestFunction { get; set; }
        private Thread _thread;
        private double TimeToWait { get; set; }
        private readonly HttpClient _client = new HttpClient { BaseAddress = new Uri("http://localhost:2218/") };

        public Client(Func<HttpClient, Boolean> threadStart, int t)
        {
            RequestFunction = threadStart;
            _upper *= t;
            _lower *= t;
            var r = new Random();
            TimeToWait = _lower + (r.NextDouble() * (_upper - _lower));
            
        }

        public void StartClient()
        {
            _thread = new Thread(SendRequest);
            _thread.Start();
        }

        private void SendRequest()
        {
            Thread.Sleep(500);
            // Add an Accept header for JSON format.
            _client.DefaultRequestHeaders.Accept.Add(
                    new MediaTypeWithQualityHeaderValue("application/json"));
            var response = _client.GetAsync("").Result;
            while (RequestFunction(_client))
            {
                Thread.Sleep((int) TimeToWait);
            }
        }
    }
}
